using System.Collections.Generic;
using DL.Tools;
using Message.Base;

namespace Logic.Protocal.MessagePool
{
 public static class MessagePool
 {
  private static ObjectPool<NetSystem.QuitMessage> QuitMessage_pools =
   new(() => new(QuitMessage_ID), null, (msg) => msg.Reset(), null, true, 10, 25);

  private static ObjectPool<NetSystem.HeartMessage> HeartMessage_pools =
   new(() => new(HeartMessage_ID), null, (msg) => msg.Reset(), null, true, 10, 25);

  private static ObjectPool<NetGameRunning.ChatMessage> ChatMessage_pools =
   new(() => new(ChatMessage_ID), null, (msg) => msg.Reset(), null, true, 10, 25);

  private static ObjectPool<NetGameRunning.RequestMessage> RequestMessage_pools =
   new(() => new(RequestMessage_ID), null, (msg) => msg.Reset(), null, true, 10, 25);

  private static ObjectPool<NetGameRunning.ResponseMessage> ResponseMessage_pools =
   new(() => new(ResponseMessage_ID), null, (msg) => msg.Reset(), null, true, 10, 25);

  public const int QuitMessage_ID = 1;
  public const int HeartMessage_ID = 2;
  public const int ChatMessage_ID = 10002;
  public const int RequestMessage_ID = 10003;
  public const int ResponseMessage_ID = 10004;
  static List<int> messageIDs = new List<int> { 1, 2, 10002, 10003, 10004 };
  public static List<int> MessageIDs => messageIDs;
  public static NetSystem.QuitMessage GetQuitMessage() => QuitMessage_pools.Get();
  public static void ReturnQuitMessage(NetSystem.QuitMessage msg) => QuitMessage_pools.Return(msg);
  public static NetSystem.HeartMessage GetHeartMessage() => HeartMessage_pools.Get();
  public static void ReturnHeartMessage(NetSystem.HeartMessage msg) => HeartMessage_pools.Return(msg);
  public static NetGameRunning.ChatMessage GetChatMessage() => ChatMessage_pools.Get();
  public static void ReturnChatMessage(NetGameRunning.ChatMessage msg) => ChatMessage_pools.Return(msg);
  public static NetGameRunning.RequestMessage GetRequestMessage() => RequestMessage_pools.Get();
  public static void ReturnRequestMessage(NetGameRunning.RequestMessage msg) => RequestMessage_pools.Return(msg);
  public static NetGameRunning.ResponseMessage GetResponseMessage() => ResponseMessage_pools.Get();
  public static void ReturnResponseMessage(NetGameRunning.ResponseMessage msg) => ResponseMessage_pools.Return(msg);

  public static BaseMessage GetMessage(int id)
  {
   Debugger.EO_Log($"消息{id}获取");
   switch (id)
   {
    default: return null;
    case QuitMessage_ID: return GetQuitMessage();
    case HeartMessage_ID: return GetHeartMessage();
    case ChatMessage_ID: return GetChatMessage();
    case RequestMessage_ID: return GetRequestMessage();
    case ResponseMessage_ID: return GetResponseMessage();
   }
  }

  public static void ReturnMessage(BaseMessage msg)
  {
   var id = msg.MessageID;
   switch (id)
   {
    case QuitMessage_ID:
     ReturnQuitMessage((NetSystem.QuitMessage)msg);
     break;
    case HeartMessage_ID:
     ReturnHeartMessage((NetSystem.HeartMessage)msg);
     break;
    case ChatMessage_ID:
     ReturnChatMessage((NetGameRunning.ChatMessage)msg);
     break;
    case RequestMessage_ID:
     ReturnRequestMessage((NetGameRunning.RequestMessage)msg);
     break;
    case ResponseMessage_ID:
     ReturnResponseMessage((NetGameRunning.ResponseMessage)msg);
     break;
   }

   Debugger.EO_Log($"消息{msg.MessageID}被回收");
  }
 }
}
